package main

func longestCommonPrefix(strs []string) string {
	if len(strs) == 0 {
		return ""
	}
	length := len(strs)
	prefix := strs[0]
	for i := 1; i < length; i++ {
		prefix = lcp(prefix, strs[i])
		if len(prefix) == 0 {
			return ""
		}
	}
	return prefix
}

func lcp(a1 string, a2 string) string {
	length := min(len(a1), len(a2))
	idx := 0
	for idx < length && a1[idx] == a2[idx] {
		idx++
	}
	return a1[:idx]
}

// 基本思路：从左到右，对比每两个字符串的公共前缀，然后将结果作为下一次的对比，如果结果为空，则返回空字符串
func main() {
	strs := []string{"flower", "flower", "flower", "flower"}
	result := longestCommonPrefix(strs)
	println(result)
}
